<?php
#BEGIN_LICENSE
#-------------------------------------------------------------------------
# Module: CGBlog (c) 2010-2013 by Robert Campbell 
#         (calguy1000@cmsmadesimple.org)
#  An addon module for CMS Made Simple to allow creation, management of
#  and display of blog articles.
# 
#  This module forked from the original CMSMS News Module (c) 
#  Ted Kulp, and Robert Campbell.
# 
#-------------------------------------------------------------------------
# CMS - CMS Made Simple is (c) 2005 by Ted Kulp (wishy@cmsmadesimple.org)
# This project's homepage is: http://www.cmsmadesimple.org
#
#-------------------------------------------------------------------------
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# However, as a special exception to the GPL, this software is distributed
# as an addon module to CMS Made Simple.  You may not use this software
# in any Non GPL version of CMS Made simple, or in any version of CMS
# Made simple that does not indicate clearly and obviously in its admin 
# section that the site was built with CMS Made simple.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# Or read it online: http://www.gnu.org/licenses/licenses.html#GPL
#
#-------------------------------------------------------------------------
#END_LICENSE

$cgextensions = cms_join_path($gCms->config['root_path'],'modules','CGExtensions','CGExtensions.module.php');
if( !is_readable( $cgextensions ) ) {
  echo '<h1><font color="red">ERROR: The CGExtensions module could not be found.</font></h1>';
  return;
}
require_once($cgextensions);

final class CGBlog extends CGExtensions
{
  public function GetName() { return 'CGBlog'; }
  public function IsPluginModule() { return true; }
  public function HasAdmin() { return true; }
  public function LazyLoadAdmin() { return TRUE; }
  public function GetVersion() { return '1.10.3'; }
  public function MinimumCMSVersion() { return '1.11.7'; }
  public function GetAdminDescription() { return $this->Lang('description'); }
  public function GetAdminSection() { return 'content'; }
  public function AllowAutoInstall() { return FALSE; }
  public function AllowAutoUpgrade() { return FALSE; }
  public function InstallPostMessage() { return $this->Lang('postinstall'); }
  public function GetHelp() { return @file_get_contents(__DIR__.'/help.inc'); }
  public function GetAuthor() { return 'Robert Campbell'; }
  public function GetAuthorEmail() { return 'calguy1000@cmsmadesimple.org'; }
  public function GetChangeLog() { return file_get_contents(__DIR__.'/changelog.inc'); }
  public function GetEventDescription( $eventname ) { return $this->lang('eventdesc-' . $eventname); }
  public function GetEventHelp( $eventname ) { return $this->lang('eventhelp-' . $eventname); }
  public function GetDependencies() { return array('CGExtensions'=>'1.37.2','CGSimpleSmarty'=>'1.7'); }
  public function UninstallPreMessage() { return $this->Lang('really_uninstall'); }

  public function __construct()
  {
    parent::__construct();
    $this->RegisterModulePlugin();

    global $CMS_ADMIN_PAGE;
    if( 1 ) {
      $db = cmsms()->GetDb();
      $qparms = array('');
      $query = 'SELECT cgblog_id,url FROM '.cms_db_prefix().'module_cgblog WHERE url != ?';
      if( !isset($CMS_ADMIN_PAGE) ) {
	// for non admin requests we only need to register routes to published articles.
	$query .= ' AND status = ?';
	$qparms[] = 'published';
      }
      $query .= ' AND COALESCE(start_time,@CG_ZEROTIME) < NOW()';
      $query .= ' AND COALESCE(end_time,@CG_FUTURETIME) > NOW()';
      $query .= ' ORDER BY cgblog_date DESC';

      $tmp = $db->GetArray($query,$qparms);
      if( is_array($tmp) ) {
	$detailpage = $this->GetPreference('default_detailpage',-1);
	if( $detailpage == -1 ) {
	  $contentops = cmsms()->GetContentOperations();
	  $detailpage = $contentops->GetDefaultContent();
	}
	foreach( $tmp as $one ) {
	  $parms = array('action'=>'detail','returnid'=>$detailpage,'articleid'=>$one['cgblog_id']);
	  $route = new CmsRoute($one['url'],$this->GetName(),$parms,TRUE);
	  cms_route_manager::register($route);
	}
      }
    }
  }

  public function GetFriendlyName()
  {
    $tmp = $this->GetPreference('friendlyname');
    if( !$tmp ) $tmp = $this->Lang('friendlyname');
    return $tmp;
  }

  public function SetParameters()
  {
    $this->RestrictUnknownParams();

    $summarypage = $this->GetPreference('default_summarypage',-1);
    if( $summarypage == -1 ) {
      $contentops = cmsms()->GetContentOperations();
      $summarypage = $contentops->GetDefaultPageID();
    }
    $detailpage = $this->GetPreference('default_detailpage',-1);
    if( $detailpage == -1 ) {
      $contentops = cmsms()->GetContentOperations();
      $detailpage = $contentops->GetDefaultPageID();
    }

    $str = '[cC][Gg][Bb]log';
    $str = $this->GetPreference('urlprefix',$str);
    $this->RegisterRoute('/'.$str.'\/archive\/(?P<year>[0-9]+)\/(?P<month>[0-9]+)\/(?P<returnid>[0-9]+)$/', array('action'=>'default'));
    $this->RegisterRoute('/'.$str.'\/archive\/(?P<year>[0-9]+)\/(?P<month>[0-9]+)$/', array('action'=>'default','returnid'=>$summarypage));
    $this->RegisterRoute('/'.$str.'\/(?P<articleid>[0-9]+)\/(?P<returnid>[0-9]+)\/(?P<junk>.*?)$/',
			 array('action'=>'detail'));
    $this->RegisterRoute('/'.$str.'\/(?P<articleid>[0-9]+)\/(?P<returnid>[0-9]+)$/', array('action'=>'detail'));
    $this->RegisterRoute('/'.$str.'\/(?P<articleid>[0-9]+)\/(?P<junk>.*?)$/', array('action'=>'detail','returnid'=>$detailpage));
    $this->RegisterRoute('/'.$str.'\/(?P<articleid>[0-9]+)$/', array('action'=>'detail','returnid'=>$detailpage));
    $this->RegisterRoute('/'.$str.'\/category\/(?P<category_id>[0-9]+)\/(?P<returnid>[0-9]+)\/(?P<junk>.*?)$/',
			 array('action'=>'default'));
    $this->RegisterRoute('/'.$str.'\/category\/(?P<category_id>[0-9]+)\/(?P<junk>.*?)$/', array('action'=>'default','returnid'=>$summarypage));

    $this->SetParameterType('year',CLEAN_INT);
    $this->CreateParameter('year','',$this->Lang('help_year'));
    $this->SetParameterType('month',CLEAN_INT);
    $this->CreateParameter('month','',$this->Lang('help_month'));

    $this->CreateParameter('pagelimit', 100000, $this->Lang('help_pagelimit'));
    $this->CreateParameter('showdraft',0,$this->Lang('helpshowdraft'));
    $this->SetParameterType('showdraft',CLEAN_INT);

    $this->CreateParameter('searchexpired',0,$this->Lang('helpsearchexpired'));
    $this->SetParameterType('searchexpired',CLEAN_INT);

    $this->CreateParameter('showall', 0, $this->Lang('helpshowall'));
    $this->CreateParameter('showarchive', 0, $this->Lang('helpshowarchive'));
    $this->CreateParameter('sortasc', 'true', $this->Lang('helpsortasc'));
    $this->CreateParameter('sortby', 'cgblog_date', $this->Lang('helpsortby'));
    $this->CreateParameter('detailpage', 'pagealias', $this->Lang('helpdetailpage'));
    $this->CreateParameter('detailtemplate', '', $this->Lang('helpdetailtemplate'));
    $this->CreateParameter('summarypage', '', $this->Lang('helpsummarypage'));
    $this->CreateParameter('summarytemplate', '', $this->Lang('helpsummarytemplate'));
    $this->CreateParameter('browsecattemplate', '', $this->Lang('helpbrowsecattemplate'));
    $this->CreateParameter('felisttemplate', '', $this->Lang('helpfelisttemplate'));
    $this->CreateParameter('fesubmittemplate', '', $this->Lang('helpfesubmittemplate'));
    $this->CreateParameter('archivetemplate', '', $this->Lang('helparchivetemplate'));
    $this->CreateParameter('category', '', $this->Lang('helpcategory'));
    $this->CreateParameter('notcategory', '', $this->Lang('helpnotcategory'));
    $this->CreateParameter('number', 100000, $this->Lang('helpnumber'));
    $this->CreateParameter('start', 0, $this->Lang('helpstart'));
    $this->CreateParameter('action','default',$this->Lang('helpaction'));
    $this->CreateParameter('articleid','',$this->Lang('help_articleid'));

    $this->SetParameterType('pagelimit',CLEAN_INT);
    $this->SetParameterType('browsecat',CLEAN_INT);
    $this->SetParameterType('showall',CLEAN_INT);
    $this->SetParameterType('showarchive',CLEAN_INT);
    $this->SetParameterType('sortasc',CLEAN_STRING); // should be int, or boolean
    $this->SetParameterType('sortby',CLEAN_STRING);
    $this->SetParameterType('detailpage',CLEAN_STRING);
    $this->SetParameterType('detailtemplate',CLEAN_STRING);
    $this->SetParameterType('browsecattemplate',CLEAN_STRING);
    $this->SetParameterType('fesubmittemplate',CLEAN_STRING);
    $this->SetParameterType('felisttemplate',CLEAN_STRING);
    $this->SetParameterType('archivetemplate',CLEAN_STRING);
    $this->SetParameterType('summarypage',CLEAN_STRING);
    $this->SetParameterType('summarytemplate',CLEAN_STRING);
    $this->SetParameterType('category',CLEAN_STRING);
    $this->SetParameterType('notcategory',CLEAN_STRING);
    $this->SetParameterType('category_id',CLEAN_STRING);
    $this->SetParameterType('number',CLEAN_INT);
    $this->SetParameterType('start',CLEAN_INT);
    $this->SetParameterType('pagenumber',CLEAN_INT);
    $this->SetParameterType('articleid',CLEAN_INT);
    $this->SetParameterType('origid',CLEAN_INT);
    $this->SetParameterType('returid',CLEAN_INT);
    $this->SetParameterType('showtemplate',CLEAN_STRING);
    $this->SetParameterType('assign',CLEAN_STRING);
    $this->SetParameterType('inline',CLEAN_STRING);
    $this->SetParameterType('uglyurls',CLEAN_INT);
    $this->SetParameterType('preview',CLEAN_INT);
    $this->CreateParameter('uglyurls',0,$this->Lang('helpuglyurls'));
    $this->CreateParameter('inline','false',$this->Lang('help_inline'));
    $this->SetParameterType('fesubmitpage',CLEAN_STRING);
    $this->CreateParameter('fesubmitpage','',$this->Lang('help_fesubmitpage'));
    $this->SetParameterType('author',CLEAN_STRING);
    $this->Createparameter('author','',$this->Lang('help_userparam'));
    $this->SetParameterType('admin_user',CLEAN_STRING);
    $this->Createparameter('admin_user','',$this->Lang('help_adminuser'));
    $this->SetParameterType(CLEAN_REGEXP.'/cgblog_.*/',CLEAN_STRING);
    $this->SetParameterType('junk',CLEAN_STRING);
  }

  public function VisibleToAdminUser()
  {
    return $this->CheckPermission('Modify CGBlog') || 
      $this->CheckPermission('Modify Site Preferences') || 
      $this->CheckPermission('Modify Templates') || 
      $this->CheckPermission('Approve CGBlog');
  }

  public function UpdateHierarchyPositions()
  {
      $db = $this->GetDb();

      $query = "SELECT cgblog_category_id, cgblog_category_name FROM ".cms_db_prefix()."module_cgblog_categories";
      $dbresult = $db->Execute($query);
      while ($dbresult && $row = $dbresult->FetchRow()) {
	$current_hierarchy_position = "";
	$current_long_name = "";
	$content_id = $row['cgblog_category_id'];
	$current_parent_id = $row['cgblog_category_id'];
	$count = 0;
	  
	while ($current_parent_id > -1) {
	  $query = "SELECT cgblog_category_id, cgblog_category_name, parent_id FROM ".cms_db_prefix()."module_cgblog_categories WHERE cgblog_category_id = ?";
	  $row2 = $db->GetRow($query, array($current_parent_id));
	  if ($row2) {
	    $current_hierarchy_position = str_pad($row2['cgblog_category_id'], 5, '0', STR_PAD_LEFT) . "." . $current_hierarchy_position;
	    $current_long_name = $row2['cgblog_category_name'] . ' | ' . $current_long_name;
	    $current_parent_id = $row2['parent_id'];
	    $count++;
	  }
	  else {
	    $current_parent_id = 0;
	  }
	}

	if (strlen($current_hierarchy_position) > 0) {
	  $current_hierarchy_position = substr($current_hierarchy_position, 0, strlen($current_hierarchy_position) - 1);
	}

	if (strlen($current_long_name) > 0) $current_long_name = substr($current_long_name, 0, strlen($current_long_name) - 3);

	$query = "UPDATE ".cms_db_prefix()."module_cgblog_categories SET hierarchy = ?, long_name = ? WHERE cgblog_category_id = ?";
	$db->Execute($query, array($current_hierarchy_position, $current_long_name, $content_id));
      }
  }

  function CreateParentDropdown($id, $catid = -1, $selectedvalue = -1)
  {
    $db = $this->GetDb();
    $longname = '';
    $items['('.$this->Lang('none').')'] = '-1';

    $query = "SELECT hierarchy, long_name FROM ".cms_db_prefix()."module_cgblog_categories WHERE cgblog_category_id = ?";
    $dbresult = $db->Execute($query, array($catid));
    while ($dbresult && $row = $dbresult->FetchRow()) {
      $longname = $row['hierarchy'] . '%';
    }

    $query = "SELECT cgblog_category_id, cgblog_category_name, hierarchy, long_name FROM ".cms_db_prefix()."module_cgblog_categories WHERE hierarchy not like ? ORDER by hierarchy";
    $dbresult = $db->Execute($query, array($longname));
    while ($dbresult && $row = $dbresult->FetchRow()) {
      $items[$row['long_name']] = $row['cgblog_category_id'];
    }

    return $this->CreateInputDropdown($id, 'parent', $items, -1, $selectedvalue);
  }


  function SearchResultWithParams($returnid, $articleid, $attr = '', $params = '')
  {
    $gCms = cmsms();
    $result = array();

    if ($attr == 'cgblog') {
      $db = $this->GetDb();
      $q = "SELECT cgblog_id,cgblog_title,url FROM ".cms_db_prefix()."module_cgblog WHERE cgblog_id = ?";
      if( (isset($params['searchexpired']) && (int)$params['searchexpired'] == 1) || $this->GetPreference('expired_searchable',1) == 1 ) {
	// nothing here.
      }
      else {
	// make sure we don't return expired articles.
	// if we don't want em to.
	$now = $db->DbTimeStamp(time());
	$zerotime = $db->DbTimeStamp(1);
	$q .= " AND ((".$db->IfNull('end_time',$zerotime)." = ".$zerotime.") OR (end_time > $now))";
      }
      $row = $db->GetRow( $q, array( (int)$articleid ) );
	  
      if ($row) {
	//0 position is the prefix displayed in the list results.
	$result[0] = $this->GetFriendlyName();

	//1 position is the title
	$result[1] = $row['cgblog_title'];

	//2 position is the URL to the title.
	$detailpage = cgblog_utils::get_detailpage($returnid,$params);
	$prettyurl = $row['url'];
	if( $prettyurl == '' ) {
	  $aliased_title = munge_string_to_url($row['cgblog_title']);
	  if( $this->GetPreference('default_detailpage',-1) != -1 && !isset($params['detailpage']) ) {
	    $prettyurl = $this->GetPreference('urlprefix','cgblog').'/'.$row['cgblog_id']."/$aliased_title";
	  }
	  else {
	    $prettyurl = $this->GetPreference('urlprefix','cgblog').'/'.$row['cgblog_id'].'/'.$detailpage."/$aliased_title";
	  }
	}

	$result[2] = $this->CreateLink('cntnt01', 'detail', $detailpage, '', array('articleid' => $articleid) ,'', true, false, '', true, $prettyurl);
      }
      return $result;
    }
  }
    
  function SearchReindex(&$module)
  {
    $db = $this->GetDb();
    $query = 'SELECT * FROM '.cms_db_prefix()."module_cgblog WHERE status = 'published'";
    if( $this->GetPreference('expired_searchable',1) == 0 ) {
      // make sure we don't return expired articles.
      // if we don't want em to.
      $now = $db->DbTimeStamp(time());
      $zerotime = $db->DbTimeStamp(1);
      $query .= " AND ((".$db->IfNull('end_time',$zerotime)." = ".$zerotime.") OR (end_time > $now))";
    }
    $query .= ' ORDER BY cgblog_date';
    $result = &$db->Execute($query);
    while ($result && !$result->EOF) {
      $text = $result->fields['cgblog_data'] . ' ' . $result->fields['summary'] . ' ' . $result->fields['cgblog_title'] . ' ' . $result->fields['cgblog_title'];

      $fq = 'SELECT fd.id,fd.name,fd.type,fv.cgblog_id,fv.value FROM '.cms_db_prefix().'module_cgblog_fielddefs AS fd
             LEFT JOIN '.cms_db_prefix().'module_cgblog_fieldvals AS fv
             ON fd.id = fv.fielddef_id
             WHERE fd.public = 1 AND fv.cgblog_id = ?';
      $fields = $db->GetArray($fq,array($result->fields['cgblog_id']));
      if( $fields ) {
	foreach ( $fields as $one ) {
	  if( strlen($one['value']) <= 1 ) continue;
	  $text .= ' '.$one['value'];
	}
      }

      $module->AddWords($this->GetName(), 
			$result->fields['cgblog_id'], 'cgblog', 
			$text,
			($result->fields['end_time'] != NULL && $this->GetPreference('expired_searchable',1) == 0) ?  $db->UnixTimeStamp($result->fields['end_time']) : NULL);
      $result->MoveNext();
    }
    if( $result ) $result->Close();
  }

  /**
   * An api function (callable from user tags) that allows the creation
   * of a new cgblog article in a named category.
   *
   * AuthorID, is taken from the currently logged in ID, or is set at 0
   */
  function AddNewArticle( $category, $title, $summary, $text = null, $start_time = null, $end_time = null, $status = 'published', $icon = null )
  {
    $db = $this->GetDb();

    // find the category id
    $q = "SELECT cgblog_category_id AS id FROM ".cms_db_prefix()."module_cgblog_categories
		WHERE cgblog_category_name = ?";
    $dbresult = $db->Execute( $q, array( $category ) );
    if( !$dbresult || $dbresult->RecordCount() == 0 ) return array( FALSE, $this->Lang('error_api_nocategory') );
    $row = $dbresult->FetchRow();
    $catid = $row['id'];

    // find the author id given a name
    $authorid = 0;
    if( get_userid() != false ) $authorid = get_userid();

    // check the remaining params
    if( $summary == '' ) return array( FALSE, $this->Lang('error_api_nosummary') );

    if( $start_time == null ) $start_time = trim($db->DBTimeStamp(time()), "'");

    // get a new article id
    $cgblogid = $db->GenID(cms_db_prefix()."module_cgblog_seq");

    // prepare the query
    $query = 'INSERT INTO '.cms_db_prefix().'module_cgblog ( CGBLOG_ID, CGBLOG_CATEGORY_ID, AUTHOR_ID, CGBLOG_TITLE, CGBLOG_DATA, CGBLOG_DATE, SUMMARY, START_TIME, END_TIME, STATUS, ICON, CREATE_DATE, MODIFIED_DATE ) VALUES (?,?,?,?,?,?,?,?,?,?,?,'.$db->DBTimeStamp(time()).','.$db->DBTimeStamp(time()).')';
    $parms = array( $cgblogid, $catid, $authorid, $title, $text, $start_time, $summary, $start_time, $end_time, $status, $icon);

    // and go
    $dbresult = $db->Execute( $query, $parms );
    if( !$dbresult ) return array( FALSE, $this->Lang('error_api_dberror') );

    @$this->SendEvent('CGBlogArticleAdded', array('cgblog_id' => $cgblogid, 'category_id' => $catid, 'title' => $title, 'content' => $text, 'summary' => $summary, 'status' => $status, 'start_time' => $start_time, 'end_time' => $end_time));

    return array( TRUE );
  }


  function myRedirectToTab( $id, $tab, $params = '' )
  {
    $parms = array();
    if( is_array( $params ) ) $parms = $params;
    $parms = array('active_tab' => $tab );
    $this->myRedirect( $id, 'defaultadmin', $parms );
  }

  function myRedirect( $id, $action, $params = '' )
  {
    unset( $params['action'] );
    $this->Redirect( $id, $action, '', $params );
  }


  public function get_field_types()
  {
    $items = array('textbox'=>$this->Lang('textbox'),
		   'checkbox'=>$this->Lang('checkbox'),
		   'textarea'=>$this->Lang('textarea'),
		   'file'=>$this->Lang('file'),
		   'image'=>$this->Lang('image'));

    return $items;
  }

  /**
   * @deprecated
   */
  function GetTypesDropdown( $id, $name, $selected = '' )
  {
    $items = array_flip($this->get_field_types());
    return $this->CreateInputDropdown($id, $name, $items, -1, $selected);
  }


  function handle_upload($itemid, $fieldname, &$error)
  {
    $config = cmsms()->GetConfig();

    $p = cms_join_path($config['uploads_path'],'cgblog');
    if (!is_dir($p)) {
      $res = @mkdir($p);
      if( $res === FALSE ) {
	$error = $this->Lang('error_mkdir',$p);
	return FALSE;
      }
    }

    $p = cms_join_path($config['uploads_path'],'cgblog','id'.$itemid);
    if (!is_dir($p)) {
      if( @mkdir($p) === FALSE ) {
	$error = $this->Lang('error_mkdir',$p);
	return FALSE;
      }
    }

    if( $_FILES[$fieldname]['size'] > $config['max_upload_size'] ) {
      $error = $this->Lang('error_filesize');
      return FALSE;
    }

    $filename = basename($_FILES[$fieldname]['name']);
    $dest = cms_join_path($config['uploads_path'],'cgblog','id'.$itemid,$filename);

    // Get the files extension
    $ext = substr(strrchr($filename, '.'), 1);

    // compare it against the 'allowed extentions'
    $exts = explode(',',$this->GetPreference('allowed_upload_types',''));
    if( !in_array( $ext, $exts ) ) {
      $error = $this->Lang('error_invalidfiletype');
      return FALSE;
    }

    if( @cms_move_uploaded_file($_FILES[$fieldname]['tmp_name'], $dest) === FALSE ) {
      $error = $this->Lang('error_movefile',$dest);
      return FALSE;
    }

    return $filename;
  }

  function get_category_list()
  {
    $db = $this->GetDb();
    $categorylist = array();
    $query = "SELECT * FROM ".cms_db_prefix()."module_cgblog_categories ORDER BY sort_order";
    $dbresult = $db->Execute($query);

    while ($dbresult && $row = $dbresult->FetchRow()) {
      $categorylist[$row['name']] = $row['id'];
    }

    return $categorylist;
  }

  function delete_article($articleid)
  {
    $gCms = cmsms();
    $db = $this->GetDb();

    //Now remove the article
    $query = "DELETE FROM ".cms_db_prefix()."module_cgblog WHERE cgblog_id = ?";
    $db->Execute($query, array($articleid));

    // Delete it from the categories
    $query = 'DELETE FROM '.cms_db_prefix().'module_cgblog_blog_categories WHERE blog_id = ?';
    $db->Execute($query, array($articleid));
    
    // Delete it from the custom fields
    $query = 'DELETE FROM '.cms_db_prefix().'module_cgblog_fieldvals WHERE cgblog_id = ?';
    $db->Execute($query, array($articleid));
    
    // Delete any files.
    $config = $gCms->GetConfig();
    $dir = cms_join_path($config['uploads_path'],'cgblog','id'.$articleid);
    cge_dir::recursive_remove_directory($dir);

    //Update search index
    $module = $this->GetModuleInstance('Search');
    if ($module != FALSE) $module->DeleteWords($this->GetName(), $articleid, 'cgblog');

    @$this->SendEvent('CGBlogArticleDeleted', array('cgblog_id' => $articleid));
  }

  function GetNotificationOutput($priority = 2)
  {
    // if this user has permission to change CGBlog articles from
    // Draft to published, and there are draft cgblog articles
    // then display a nice message.
    // this is a priority 2 item.
    if( $priority >= 2 ) {
      $output = array();
      if( $this->CheckPermission('Approve CGBlog') ) {
	$gCms = cmsms();
	$db = $gCms->GetDb();
	$query = 'SELECT count(cgblog_id) FROM '.cms_db_prefix().'module_cgblog n
                  WHERE status != \'published\'
                 AND (('.$db->IfNull('end_time',$db->DbTimeStamp(1)).' = '.$db->DbTimeStamp(1).') OR (end_time > '.$db->DbTimeStamp(time()).')) ';
	$count = $db->GetOne($query);
	if( $count ) {
	  $obj = new StdClass;
	  $obj->priority = 2;
	  $link = $this->CreateLink('m1_','defaultadmin','',$this->Lang('notify_n_draft_items_sub',$count));
	  $obj->html = $this->Lang('notify_n_draft_items',$link);
	  $output[] = $obj;
	}
      }
    }
    return $output;
  }

  function SuppressAdminOutput(&$request)
  {
    $action = '';
    if( isset($params['action']) ) {
      $action = trim($params['action']);
    }
    else if( isset($params['mact']) ) {
      $tmp = explode(',',$params['mact']);
      $action = $tmp[2];
    }

    if( startswith($action,'ajax') ) return true;

    return false;
  }

  public function IsValidRoute($page,$src_module = '')
  {
    $gCms = cmsms();
    $db = $gCms->GetDb();

    $query = 'SELECT cgblog_id FROM '.cms_db_prefix().'module_cgblog WHERE url = ?';
    $res = $db->GetOne($query,array($page));
    if( $res ) {
      $tmp = array();
      $tmp['action'] = 'detail';
      $tmp['articleid'] = $res;

      $returnid = $this->GetPreference('default_detailpage');
      if( $returnid == '' || $returnid == -1 ) {
	$contentops = $gCms->GetContentOperations();
	$returnid = $contentops->GetDefaultContent();
      }
      $tmp['returnid'] = $returnid;
      return $tmp;
    }
    return FALSE;
  }

  public function HasCapability($capability, $params = array())
  {
    if( $capability == 'cg_socialblast_feeder' ) return TRUE;
    return FALSE;
  }

  public function GetHeaderHTML()
  {
    $obj = cms_utils::get_module('JQueryTools','1.2');
    if( is_object($obj) ) {
      $tmpl = <<<EOT
{JQueryTools action='require' lib='tablesorter,jquerytools,cgform'}
{JQueryTools action='placemarker'}
EOT;
      $txt = $this->ProcessTemplateFromData($tmpl);
      return $txt;
    }
  }

  protected function CreateUserMultiselect($id, $selected = '', $name='ownerid', 
					   $groups = true, $size=5)
  {
    $gCms = cmsms();
    $result = '';

    $result .= '<select multiple size="'.$size.'" name="'.$id.$name.'[]">';
    if( $groups ) {
      $groupops = $gCms->GetGroupOperations();
      $allgroups = $groupops->LoadGroups();
      foreach( $allgroups as $onegroup ) {
	$result .= '<option value="-'.$onegroup->id.'"';
	if( is_array($selected) && in_array($onegroup->id * -1, $selected) ) {
	  $result .= ' selected="selected"';
	}
	else if( ($onegroup->id * -1) == $selected ) {
	  $result .= ' selected="selected"';
	}
	$result .= '>'.lang('group').': '.$onegroup->name.'</option>';
      }
    }

    $userops = $gCms->GetUserOperations();
    $allusers = $userops->LoadUsers();
    if (count($allusers) > 0) {
      foreach ($allusers as $oneuser) {
	$result .= '<option value="'.$oneuser->id.'"';
	if( is_array($selected) && in_array($oneuser->id, $selected) ) {
	  $result .= ' selected="selected"';
	}
	else if( $oneuser->id == $selected ) {
	  $result .= ' selected="selected"';
	}
	$result .= '>'.$oneuser->username.'</option>';
      }
    }
    $result .= '</select>';
    
    return $result;
  }


  function CreatePrettyLink($id,$action,$returnid = '',
			    $contents = '', $params = array(),
			    $warn_message = '', $only_href = false, $inline = false, $addtext = '',
			    $targetcontentsonly = false, $prettyurl = '' )
  {
    $gCms = cmsms();
    $db = $gCms->GetDb();

    // this method, if overridden should call CreateLink for all stuff it can't understand
    $out = '';
    switch( $action ) {
    case 'detail':
      {
	$article_id = '';
	if( isset($params['articleid']) ) $article_id = (int)$params['articleid'];
	if( $article_id <= 0 ) return;

	$query = 'SELECT * FROM '.cms_db_prefix().'module_cgblog WHERE cgblog_id = ?';
	$row = $db->GetRow($query,array($article_id));
	if( !$row ) return;

	$prettyurl = $row['url'];
	if( $prettyurl == '' ) {
	  $tmp = munge_string_to_url($row['cgblog_title']);
	  $prettyurl = $this->GetPreference('urlprefix','cgblog')."/{$row['cgblog_id']}/{$returnid}/{$tmp}";
	  if( $this->GetPreference('default_detailpage',-1) != -1 ) {
	    $prettyurl = $this->GetPreference('urlprefix','cgblog')."/{$row['cgblog_id']}/{$tmp}";
	  }
	}
	$out = $this->CreateLink($id,'detail',$returnid,$contents,$params,$warn_message,
				 $only_href,$inline,$addtext,$targetcontentsonly,$prettyurl);
      }
      break;

    default:
      $out = $this->CreateLink($id,$action,$returnid,$contents,$params,$warn_message,
			       $only_href,$inline,$addtext,$targetcontentsonly,$prettyurl);
    }

    return $out;
  }

  public function &get_blaster_content($articleid)
  {
    $article = cgblog_article::get_by_id($articleid);
    if( !is_object($article) ) throw new CmsInvalidDataException($this->Lang('error_getarticle'));
    if( $article->status != 'published' ) throw new CmsInvalidDataException($this->Lang('error_getarticle'));

    // note, this prepares a summary of the article for use in sending to social media.
    // later we may want to do things like process stuff through a template
    $res = new sb_content;
    $res->linktitle = $article->title;
    $res->summary = ($article->summary != '')?$article->summary:$article->content;
    $res->canonical = $article->canonical;
    // here we could extract links from the summary, or content... (based on a policy?)
    
    // get image fields, and add pictures.
    $fields = $article->fields;
    if( is_array($fields) && count($fields) ) {
      foreach( $fields as $name => $obj ) {
	if( $obj->type != 'image' ) continue;
	if( $obj->value == '' ) continue;
	$url = $article->file_location.'/'.$obj->value;
	$res->add_picture($url,$obj->value);
      }
    }

    return $res;
  }
} // end of class.

# vim:ts=4 sw=4 noet
?>